#pragma once
#ifndef __BIT_FUNC_H__
#define __BIT_FUNC_H__

#define BIT_SET(x, n) ((x) = (x) | (0x01 << (n)))
#define BIT_GET(x, n) (((x) & (0x01 << (n))) != 0)
#define BIT_CLEAR(x, n) ((x) = (x) & ~(0x01 << (n)))

#define ARRAY_BIT_SIZE(bit_index) (((bit_index) + 7) / 8)

//读取缓冲区的指定位. bit_index 从0开始
#define ARRAY_GET_BIT(p_array, bit_index) ((p_array[(bit_index) >> 3] >> (((bit_index)&0x07))) & 0x01)

//设置缓冲区的指定位. bit_index 从0开始
#define ARRAY_SET_BIT(p_array, bit_index, bit_val)                                                                                                                                                     \
    if (0 != (bit_val))                                                                                                                                                                                \
    {                                                                                                                                                                                                  \
        p_array[(bit_index) >> 3] |= 0x01 << ((bit_index)&0x07);                                                                                                                                       \
    }                                                                                                                                                                                                  \
    else                                                                                                                                                                                               \
    {                                                                                                                                                                                                  \
        p_array[(bit_index) >> 3] &= ~(0x01 << ((bit_index)&0x07));                                                                                                                                    \
    }

#define ARRAY_DOUBLE_BIT_SIZE(bit_index) (2 * (bit_index) + 7) / 8

#define ARRAY_GET_DOUBLE_BIT(p_array, bit_index) (INT8U)(p_array[((bit_index)*2) >> 3] << (6 - ((bit_index)*2 & 0x07))) >> 6

#define ARRAY_SET_DOUBLE_BIT(p_array, bit_index, val)                                                                                                                                                  \
    {                                                                                                                                                                                                  \
        p_array[((bit_index)*2) >> 3] &= ~(0x03 << ((bit_index)*2 & 0x07));                                                                                                                            \
        p_array[((bit_index)*2) >> 3] |= ((val) << ((bit_index)*2 & 0x07));                                                                                                                            \
    }

#endif // __BIT_FUNC_H__
